<template>
  <a-modal
    :title="formTile + '数据字典'"
    :visible="true"
    width="50%"
    :footer="null"
    @cancel="reset"
  >
    <div class="main">

        <a-row
          type="flex"
          justify="start"
          align="top"
        >
          <a-col :span="24">
            <a-form
              :form="form"
              @submit="handleSubmit"
              :labelCol="formItemLayout.labelCol"
              :wrapperCol="formItemLayout.wrapperCol"
            >
              <a-form-item
                label="名称"
                :required="true"
              >
                <a-input
                  allow-clear
                  placeholder="请输入名称"
                  :maxLength="40"
                  :disabled="disabled"
                  v-decorator="[
                  `name`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        whitespace: true,
                        message: '请输入名称'
                      }
                    ]
                  }
                ]"
                />
              </a-form-item>
              <a-form-item
                label="字典类型"
                :required="true"
              >
              <a-select
                show-search
                style="width: 180px"
                placeholder="请选择"
                allow-clear
                v-decorator="[
                  `dictType`,
                  {
                    rules: [
                      {
                        required: true,
                        message: '请选择'
                      }
                    ]
                  }
                ]"
              >
              <a-select-option
                  :value="item.code"
                  v-for="(item,index) in typeList"
                  :key="index"
                >
                  {{ item.name }}
                </a-select-option>
                
              </a-select>
              </a-form-item>
              <a-form-item
                label="描述"
                :required="true"
              >
                <a-textarea
                  allow-clear
                  placeholder="请输入描述"
                  :rows="5"
                  :maxLength="150"
                  v-decorator="[
                  `description`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        whitespace: true,
                        message: '请输入描述'
                      }
                    ]
                  }
                ]"
                ></a-textarea>
              </a-form-item>
              <a-row>
                <a-col
                  :span="16"
                  :offset="8"
                  class="info-action"
                >
                  <a-button
                    type="primary"
                    @click="reset"
                  >返回</a-button>
                  <a-button
                    type="primary"
                    class="ml-20"
                    html-type="submit"
                    :loading="loading"
                  >保存</a-button>
                </a-col>
              </a-row>
            </a-form>
          </a-col>
        </a-row>

    </div>

  </a-modal>
</template>

<script>
import { dictCreate, dictUpdate ,getDictChildList} from '@api/rd/index'

export default {
  components: {},

  props: {
    dict: {
      type: Object,
      default: null
    }
  },

  data() {
    return {
      id: '',
      loading: false,
      idEdit: false,
      disabled:false,
      groupName:'',
      typeList:[],
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: {
          xl: { span: 5 },
          xs: { span: 5 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xl: { span: 10 },
          xs: { span: 16 },
          sm: { span: 16 }
        }
      }
    }
  },
  computed: {
    userInfo() {
      return this.$store.getters.userInfo || null
    }
  },
  created() {
    let pathname = window.location.pathname;
     const newArr = pathname.split('/')
     this.groupName = newArr[3]
    const { dict } = this.$props
    if (dict) {
      this.formTile = '编辑'
      this.id = dict.id
      this.idEdit = true
      this.disabled = true
      this.$nextTick(() => {
        this.form.setFieldsValue({ name: dict.name,dictType:dict.dictType, description: dict.description })
      })
    } else {
      this.formTile = '新增'
    }
    this.loadDetail()
  },
  methods: {
    reset() {
      this.form.resetFields()
      this.$emit('close')
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.submit(values)
        }
      })
    },
    async loadDetail() {
      const params = {
        current: 1,
        name: 'DICT_TYPE'
      }
      try {
        const res = await getDictChildList('dict', params)
        if (res.code == 200) {
          this.typeList = res.data
        } else {
          this.$message.error(res.msg)
        }
      } catch (error) {
        this.$message.error('获取字典类型失败')
      } 
    },
    async submit(values) {
      this.loading = true
      var params = values
      const { realname } = this.userInfo
      params.operator = realname
      params.groupName = this.groupName
      if (this.id) {
        params.id = this.id
      }
      try {
        const res = this.idEdit ? await dictUpdate('dict', params) : await dictCreate('dict', params)
        if (res.code == 200) {
          this.$message.success(this.idEdit ? '修改数据字典成功！' : '新建数据字典成功！')
          this.reset()
        } else {
          this.$message.warning(res.msg)
        }
      } catch (error) {
        this.$message.warning('操作失败！')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped lang="less">
.info-action {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
  // margin-bottom: 20px;
}
.ml-20 {
  margin-left: 20px;
}
</style>
